<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>商品搜索页</title>
    <style>
        * {
            list-style: none;
            margin: 0px;
            padding: 0px;
        }

        .menus {
            display: flex;
            height: 40px;
            align-items: center;
        }

        .menus li {
            border-radius: 3px;
            padding: 5px 20px;
            margin: 0px 20px;
            cursor: pointer;
        }

        .menus li.item:hover {
            background: orangered;
            color: #fff;
        }

        .menus li.item.active {
            background: orangered;
            color: #fff;
        }

        .container {
            width: 800px;
            margin: 30px auto;
            border: 1px solid gainsboro;
        }

        .goodslist {
            display: flex;
            flex-wrap: wrap
        }

        .goodslist .item {
            width: 200px;
            height: 300px;
            padding: 10px;
            font-size: 14px;
            box-sizing: border-box;
        }

        .goodslist .item img {
            width: 100%;
            height: 180px;
        }
    </style>
</head>

<body>
    <div id="app">
        <div class="container">
            <ul class="menus">
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;品牌：</li>
                <li class="item" v-bind:class="{active:item.id===currentbrandid}" v-for="item in brandlist" @click="currentbrandid = item.id">
                    {{item.name}}
                </li>
            </ul>
            <ul class="menus">
                <li>适用人群：</li>
                <li class="item" v-bind:class="{active:item.id===currentpersonTypeid}" v-for="item in persontypelist"
                    @click=" currentpersonTypeid = item.id">
                    {{item.name}}
                </li>
            </ul>
            <ul class="menus">
                <li>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;风格：</li>
                <li class="item" v-bind:class="{active:item.id===currentstyleid}" v-for="item in stylelist" @click="currentstyleid = item.id">
                    {{item.name}}
                </li>
            </ul>
            <ul class="goodslist">
                <li class="item" v-for="item in goodslist">
                    <img :src="item.img">
                    <div style="height:40px;overflow:hidden">{{item.title}}</div>
                    <div style="color:red;">￥{{item.price.toFixed(2)}} </div>
                    <div>
                        <button>加入购物车</button>
                    </div>
                </li>
            </ul>
        </div>
    </div>

    <script src="https://cdn.bootcss.com/vue/2.6.4/vue.js"></script>
    <script src="https://cdn.bootcss.com/axios/0.19.0-beta.1/axios.js"></script>
    <script>
        new Vue({
            el: "#app",
            watch: {
                currentbrandid(val) {
                    this.getGoodsList()
                },
                currentpersonTypeid(val) {
                    this.getGoodsList()
                },
                currentstyleid(val) {
                    this.getGoodsList()
                }
            },
            data: {
                currentbrandid: 0,
                currentpersonTypeid: 0,
                currentstyleid: 0,
                brandlist: [],
                persontypelist: [],
                stylelist: [],
                goodslist: []
            },
            created() {
                this.getBrandList()
                this.getpersonTypeList()
                this.getStyleList()
                this.getGoodsList()
            },
            methods: {
                getBrandList() {
                    axios.get("data/brand.json").then(res => {
                        this.brandlist = res.data
                        //在开头加上全部
                        this.brandlist.unshift({
                            id: 0,
                            name: "全部"
                        })
                    })
                },
                getpersonTypeList() {
                    axios.get("data/personType.json").then(res => {
                        this.persontypelist = res.data
                        //在开头加上全部
                        this.persontypelist.unshift({
                            id: 0,
                            name: "全部"
                        })
                    })
                },
                getStyleList() {
                    axios.get("data/style.json").then(res => {
                        this.stylelist = res.data
                        //在开头加上全部
                        this.stylelist.unshift({
                            id: 0,
                            name: "全部"
                        })
                    })
                },
                getGoodsList() {
                    this.goodslist = []
                    setTimeout(() => {
                        axios.get("data/goods.json").then(res => {

                            var result = []

                            if (!this.currentbrandid && !this.currentpersonTypeid && !this.currentstyleid) {
                                result = res.data
                            } else if (this.currentbrandid && this.currentpersonTypeid &&
                                this.currentstyleid) {
                                result = res.data.filter(item => item.brandid === this.currentbrandid &&
                                    item.personTypeid === this.currentpersonTypeid && item.styleid ===
                                    this.currentstyleid)
                            } else if (this.currentbrandid && !this.currentpersonTypeid &&
                                !this.currentstyleid) {
                                result = res.data.filter(item => item.brandid === this.currentbrandid)
                            } else if (!this.currentbrandid && this.currentpersonTypeid &&
                                !this.currentstyleid) {
                                result = res.data.filter(item => item.personTypeid === this.currentpersonTypeid)
                            } else if (!this.currentbrandid && !this.currentpersonTypeid &&
                                this.currentstyleid) {
                                result = res.data.filter(item => item.styleid === this.currentstyleid)
                            } else if (this.currentbrandid && this.currentpersonTypeid &&
                                !this.currentstyleid) {
                                result = res.data.filter(item => item.brandid === this.currentbrandid &&
                                    item.personTypeid === this.currentpersonTypeid)
                            } else if (this.currentbrandid && !this.currentpersonTypeid &&
                                this.currentstyleid) {
                                result = res.data.filter(item => item.brandid === this.currentbrandid &&
                                    item.styleid === this.currentstyleid)
                            } else if (!this.currentbrandid && this.currentpersonTypeid &&
                                this.currentstyleid) {
                                result = res.data.filter(item => item.styleid === this.currentstyleid &&
                                    item.personTypeid === this.currentpersonTypeid)
                            }

                            this.goodslist = result
                        })
                    }, 500)
                }
            },
        })
    </script>
</body>

</html>